package com.oa.hyperion.api.v1;

import com.oa.hyperion.entity.assets.AssetsDetail;
import com.oa.hyperion.entity.assets.AssetsDetail;
import com.oa.hyperion.entity.assets.AssetsGeneralLedger;
import com.oa.hyperion.service.AssetsDetailService;
import com.oa.hyperion.service.AssetsGeneralLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 资产使用明细controller
 * author yuanchongwei
 * date 2022-1-21
 */
@Api(value="资产使用明细controller",tags={"资产使用明细操作接口"})
@RestController
@RequestMapping("/v1/assets/detail")
public class AssetsDetailController {
    @Autowired
    private AssetsGeneralLedgerService assetsService;
    @Autowired
    private AssetsDetailService assetsDetailService;

    @ApiOperation("资产借用")
    @PostMapping("add")
    public String addAssetsDetail(@RequestBody AssetsDetail assetsDetail){
        if (assetsDetail == null) {
            return "参数不能为空";
        }
        AssetsGeneralLedger agl = assetsService.getAssetsById(assetsDetail.getAssetId());
        if (agl == null) {
            return "参数错误，无法找到资产信息";
        }
        int statusInDB = agl.getStatus();
        //判断资产当前状态，是否=1，否则不可借用
        if (statusInDB == 4) {
            return "资产已报废";
        }
        else if(statusInDB != 1){
            return "资产当前状态不可借用，请联系管理员";
        }
        //将使用信息写入明细表中
        assetsDetailService.insert(assetsDetail);
        //更新资产总账表中状态=2
        agl.setStatus(2);
        if(assetsService.update(agl)){
            return "资产状态更新成功：借出";
        }
        else{
            return "资产状态更新失败，请手动更新";
        }
    }

    @ApiOperation("资产归还")
    @PostMapping("update")
    public String returnAssets(@RequestBody AssetsDetail assetsDetail){
        if (assetsDetail == null || assetsDetail.getReturned().equals("") || assetsDetail.getReturned() == null) {
            return "参数不能为空";
        }
        //判断资产当前状态是否为2
        AssetsGeneralLedger agl = assetsService.getAssetsById(assetsDetail.getAssetId());
        int statusInDB = agl.getStatus();
        //判断资产当前状态，是否=1，否则不可借用
        if(statusInDB != 2){
            return "资产状态不正确，请确认后再执行";
        }
        //更新归还结果
        assetsDetailService.update(assetsDetail);
        //同步更新总账表中资产状态
        agl.setStatus(1);
        if(assetsService.update(agl)){
            return "资产状态更新成功：可用";
        }
        else{
            return "资产状态更新失败，请手动更新";
        }
    }


    @ApiOperation("查询所有资产使用明细记录")
    @GetMapping("all")
    public List<AssetsDetail> getAllDetailInfo(){
        return assetsDetailService.getAllDetails();
    }

    @ApiOperation("根据资产id查询资产使用明细信息")
    @GetMapping("assetid/{assetId}")
    public List<AssetsDetail> getDetailByAssetId(@PathVariable int assetId){
        if (assetId == 0) {
            return null;
        }
        return assetsDetailService.getAssetsDetailByAssetId(assetId);
    }

    @ApiOperation("根据经办人查询资产使用明细信息")
    @GetMapping("handler/{handler}")
    public List<AssetsDetail> getDetailBy(@PathVariable String handler){
        if (handler.equals("") || handler == null) {
            return null;
        }
        return assetsDetailService.getAssetsDetailByHandler(handler);
    }

    @ApiOperation("根据申请部门查询资产使用明细信息")
    @GetMapping("applydpt/{applydpt}")
    public List<AssetsDetail> getDetailByTransferOut(@PathVariable int applydpt){
        if (applydpt == 0) {
            return null;
        }
        return assetsDetailService.getAssetsDetailByApplydpt(applydpt);
    }

    @ApiOperation("根据是否归还查询使用明细信息")
    @GetMapping("returned/{returned}")
    public List<AssetsDetail> getDetailByTransferIn(@PathVariable String returned){
        if (returned.equals("") || returned == null) {
            return null;
        }
        return assetsDetailService.getAssetsDetailByReturned(returned);
    }
}
